//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.javaobj.auth.security.jwt;

import org.springframework.security.oauth2.server.resource.web.BearerTokenResolver;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class NullableBearerTokenResolver implements BearerTokenResolver {
    private static final Pattern authorizationPattern = Pattern.compile("^Bearer (?<token>[a-zA-Z0-9-._~+/]+)=*$", 2);


    public NullableBearerTokenResolver() {

    }

    public String resolve(HttpServletRequest request) {
        return resolveFromAuthorizationHeader(request);
    }

    private static String resolveFromAuthorizationHeader(HttpServletRequest request) {
        String authorization = request.getHeader("Authorization");
            if (StringUtils.startsWithIgnoreCase(authorization, "bearer")) {
                Matcher matcher = authorizationPattern.matcher(authorization);
                if (matcher.matches()) {
                    return matcher.group("token");
                }
        }
        return null;
    }


}
